﻿#include "HnrjitManager.h"

#include "HnrjitContainer.h"
#include "HnrjitLabel.h"
#include "HnrjitButton.h"
#include "HnrjitRadioBox.h"
#include "HnrjitCheckBox.h"
#include "HnrjitMarquee.h"
#include "HnrjitProgress.h"
#include "HnrjitImaging.h"

CHnrjitManager::CHnrjitManager()
	: CHnrjitDialog()
{
}

CHnrjitManager::~CHnrjitManager()
{
	RemoveAllElement();
}

BOOL CHnrjitManager::LoadFromFile(const TCHAR* pszXmlPath, const TCHAR* pszZipPath /*= NULL*/)
{
	BOOL bResult = FALSE;
	TiXmlDocument xmlDocument;
	if (pszZipPath)
	{
		TCHAR szZipPath[MAX_PATH];
		_stprintf_s(szZipPath, _countof(szZipPath), _T("%s\\%s"), CHnrjitImaging::AppDirPath(), pszZipPath);

		HZIP hZipArchive = OpenZip(szZipPath, NULL);
		if (hZipArchive)
		{
			int nIndex;
			ZIPENTRY ZipEntry;
			if (FindZipItem(hZipArchive, pszXmlPath, true, &nIndex, &ZipEntry) == ZR_OK)
			{
				HGLOBAL hGlobal = GlobalAlloc(LMEM_FIXED, ZipEntry.unc_size);
				if (hGlobal)
				{
					if (UnzipItem(hZipArchive, nIndex, hGlobal, ZipEntry.unc_size) == ZR_OK)
					{
						if (xmlDocument.Parse((CHAR*) hGlobal, ZipEntry.unc_size) == XML_SUCCESS)
						{
							bResult = SetupXmlUi(&xmlDocument, hZipArchive);
						}
					}
					GlobalFree(hGlobal);
				}
			}

			CloseZip(hZipArchive);
		}
	}
	else
	{
		TCHAR szXmlPath[MAX_PATH];
		_stprintf_s(szXmlPath, _countof(szXmlPath), _T("%s\\%s"), CHnrjitImaging::AppDirPath(), pszXmlPath);

		CAtlArray<CHAR> baMultiByte;
		const CHAR* utf8XmlPath = CHnrjitImaging::LocalToUtf8(szXmlPath, &baMultiByte);
		if (xmlDocument.LoadFile(utf8XmlPath) == XML_SUCCESS)
		{
			bResult = SetupXmlUi(&xmlDocument, NULL);
		}
	}

	return bResult;
}

BOOL CHnrjitManager::SetupXmlUi(TiXmlDocument* xmlDocument, HZIP hZipArchive)
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());

	TiXmlElement* xmlDialog = xmlDocument->RootElement();
	if (!xmlDialog || strcmp(xmlDialog->Value(), "Dialog") != 0) return FALSE;

	RemoveAllElement();

	SetAttribute(xmlDialog, hZipArchive);

	TiXmlElement* xmlContainer = xmlDialog->FirstChildElement("Container");
	while (xmlContainer)
	{
		AddControls(xmlContainer, hZipArchive);

		xmlContainer = xmlContainer->NextSiblingElement();
	}

	return TRUE;
}

void CHnrjitManager::RemoveAllElement()
{
	ATLASSERT(m_dwThreadID == GetCurrentThreadId());
	for (size_t i = 0; i < m_arrWidgets.GetCount(); i++)
	{
		delete m_arrWidgets[i];
	}
	m_arrWidgets.RemoveAll();
}

void CHnrjitManager::AddControls(const TiXmlElement* xmlContainer, HZIP hZipRes)
{
	DWORD dwClsid;
	const CHAR* pszValue;
	CHnrjitContainer* pContainer = NULL;
	if (pszValue = xmlContainer->Attribute("Clsid"))
	{
		dwClsid = strtol(pszValue, NULL, 10);
		pContainer = CreateContainer(dwClsid);
	}
	if (!pContainer)
	{
		pContainer = new CHnrjitContainer(this);
	}

	m_arrWidgets.Add(pContainer);
	pContainer->SetAttribute(xmlContainer, hZipRes);
	const TiXmlElement* xmlControl = xmlContainer->FirstChildElement();
	while (xmlControl)
	{
		CHnrjitControl* pControl = NULL;
		if (pszValue = xmlControl->Attribute("Clsid"))
		{
			dwClsid = strtol(pszValue, NULL, 10);
			pControl = CreateControl(dwClsid);
		}
		if (!pControl)
		{
			if (strcmp(xmlControl->Value(), "Label") == 0)
			{
				pControl = new CHnrjitLabel;
			}
			else if (strcmp(xmlControl->Value(), "Button") == 0)
			{
				pControl = new CHnrjitButton;
			}
			else if (strcmp(xmlControl->Value(), "RadioBox") == 0)
			{
				pControl = new CHnrjitRadioBox;
			}
			else if (strcmp(xmlControl->Value(), "CheckBox") == 0)
			{
				pControl = new CHnrjitCheckBox;
			}
			else if (strcmp(xmlControl->Value(), "Marquee") == 0)
			{
				pControl = new CHnrjitMarquee;
			}
			else if (strcmp(xmlControl->Value(), "Progress") == 0)
			{
				pControl = new CHnrjitProgress;
			}
		}
		if (pControl)
		{
			m_arrWidgets.Add(pControl);
			pControl->SetAttribute(xmlControl, hZipRes);
			pContainer->AddControl(pControl);
		}

		xmlControl = xmlControl->NextSiblingElement();
	}
}

CHnrjitContainer* CHnrjitManager::CreateContainer(DWORD dwClsid)
{
	return NULL;
}

CHnrjitControl* CHnrjitManager::CreateControl(DWORD dwClsid)
{
	return NULL;
}
